
#include "cannonball.h"

cannonball::cannonball(const QPoint &pos,QObject *parent)
    : QObject{parent}
{
    curPos = pos;
    active = true;
}

void cannonball::ballMove(const QPoint &pos){
    int dist;
    if((dist = sqrt(pow(curPos.y() - pos.y(),2) + pow(curPos.x() - pos.x(),2))) <= BALL_SPEED + 5){
        active = false;
        return;
    }

    double angle;
    if(curPos.y() - pos.y() != 0)
        angle = atan(1.0*(pos.x()-curPos.x())/(curPos.y()-pos.y()));
    else{
        if(pos.x() - curPos.x() > 0)
        angle = 3.1415926 / 2;
        else
        angle = -3.1415926 / 2;
    }
    int move_x = BALL_SPEED * sin(angle);
    int move_y = BALL_SPEED * cos(angle);

    if(curPos.y() - pos.y() < 0){
    curPos.setX(curPos.x() - move_x);
    curPos.setY(curPos.y() + move_y);
    }
    else{
    curPos.setX(curPos.x() + move_x);
    curPos.setY(curPos.y() - move_y);}



}

bool cannonball::getActive(void){
    return active;

}
const QPoint &cannonball::getCurPos(void){
    return curPos;

}
